/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.util.List;
import javax.swing.JOptionPane;
import modelo.Pyme;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author med
 */
public class iFrmEmpresa extends javax.swing.JInternalFrame {

    Pyme cspyme = new Pyme();
    int idp = 1;//idPyme

    public iFrmEmpresa() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        chkEditar.setVisible(false);//kk
        showPyme();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGuardar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        pnlBase = new javax.swing.JPanel();
        lblUrl = new javax.swing.JLabel();
        txtUrl = new javax.swing.JTextField();
        lblEmail = new javax.swing.JLabel();
        txtMail = new javax.swing.JTextField();
        lblSlogan = new javax.swing.JLabel();
        jScrollPanelTextoOpcional = new javax.swing.JScrollPane();
        txtaSlogan = new javax.swing.JTextArea();
        lblDirectivo = new javax.swing.JLabel();
        txtDirectico = new javax.swing.JTextField();
        lblTelefono = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        lblFax = new javax.swing.JLabel();
        txtFax = new javax.swing.JTextField();
        lblMovil = new javax.swing.JLabel();
        txtMovil = new javax.swing.JTextField();
        lblDireccion = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        lblPyme = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lblRuc = new javax.swing.JLabel();
        txtRuc = new javax.swing.JTextField();
        chkEditar = new javax.swing.JCheckBox();

        setClosable(true);
        setTitle("Datos de la Empresa");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/ADM.png"))); // NOI18N
        setPreferredSize(new java.awt.Dimension(720, 450));
        setVisible(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        getContentPane().add(btnGuardar, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 380, 120, -1));

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCerrar, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 380, 110, -1));

        pnlBase.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlBase.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblUrl.setText("Portal web:");
        pnlBase.add(lblUrl, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, -1, -1));

        txtUrl.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtUrl, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 240, 530, -1));

        lblEmail.setText("E-m@il:");
        pnlBase.add(lblEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 210, 60, -1));

        txtMail.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtMail, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 210, 530, -1));

        lblSlogan.setText("Eslogan:");
        pnlBase.add(lblSlogan, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 160, -1, -1));

        txtaSlogan.setBackground(new java.awt.Color(254, 237, 214));
        txtaSlogan.setColumns(20);
        txtaSlogan.setFont(new java.awt.Font("Ubuntu", 0, 13)); // NOI18N
        txtaSlogan.setRows(5);
        jScrollPanelTextoOpcional.setViewportView(txtaSlogan);

        pnlBase.add(jScrollPanelTextoOpcional, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, 530, 44));

        lblDirectivo.setText("Gerente:");
        pnlBase.add(lblDirectivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, 70, -1));

        txtDirectico.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtDirectico, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 530, -1));

        lblTelefono.setText("Telefono:");
        pnlBase.add(lblTelefono, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 70, -1));

        txtTelefono.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtTelefono, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 130, -1));

        lblFax.setText("Fax:");
        pnlBase.add(lblFax, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 110, -1, -1));

        txtFax.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtFax, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 100, 150, -1));

        lblMovil.setText("Movil:");
        pnlBase.add(lblMovil, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 110, 50, -1));

        txtMovil.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtMovil, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 100, 150, -1));

        lblDireccion.setText("Direccion:");
        pnlBase.add(lblDireccion, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, 70, -1));

        txtDireccion.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtDireccion, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 530, -1));

        lblPyme.setText("Compañía:");
        pnlBase.add(lblPyme, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, 80, -1));

        txtNombre.setBackground(new java.awt.Color(254, 237, 214));
        txtNombre.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        pnlBase.add(txtNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 530, -1));

        lblRuc.setText("Ruc:");
        pnlBase.add(lblRuc, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, 40, -1));

        txtRuc.setBackground(new java.awt.Color(254, 237, 214));
        pnlBase.add(txtRuc, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 180, -1));

        chkEditar.setText("Editar");
        chkEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkEditarActionPerformed(evt);
            }
        });
        pnlBase.add(chkEditar, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 10, -1, -1));

        getContentPane().add(pnlBase, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 670, 310));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private Pyme getPyme() {
        Pyme p = new Pyme();
        p.setIdPyme(idp);
        p.setRuc(txtRuc.getText().trim());
        p.setNombre(txtNombre.getText().trim());
        p.setDireccion(txtDireccion.getText().trim());
        p.setTelefono(txtTelefono.getText().trim());
        p.setFax(txtFax.getText().trim());
        p.setCelular(txtMovil.getText().trim());
        p.setDirectivo(txtDirectico.getText().trim());
        p.setEslogan(txtaSlogan.getText().trim());
        p.setUrl(txtUrl.getText().trim());
        p.setMail(txtMail.getText().trim());
        return p;
    }

    private void showPyme() {
        try {
            //List<Pyme> lsPyme = cspyme.selectPyme();
            /*
             * if(lsPyme != null) {
             *
             * }else { setHabilirar(true);
            }
             */
            //Pyme p = new Pyme();
            List<Pyme> lsP = cspyme.selectPyme();
            if (!lsP.isEmpty()) {
                for (Pyme p : lsP) {
                    idp = p.getIdPyme();
                    txtRuc.setText(p.getRuc());
                    txtNombre.setText(p.getNombre());
                    txtDireccion.setText(p.getDireccion());
                    txtTelefono.setText(p.getTelefono());
                    txtFax.setText(p.getFax());
                    txtMovil.setText(p.getCelular());
                    txtDirectico.setText(p.getDirectivo());
                    txtaSlogan.setText(p.getEslogan());
                    txtUrl.setText(p.getUrl());
                    txtMail.setText(p.getMail());
                }
                setHabilirar(false);
                chkEditar.setVisible(true);
                chkEditar.setSelected(false);
                btnGuardar.setText("Actualizar");
            }




        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Se ha producido la excepcion: \n" + ex.getMessage(), "Mostrar datos", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setHabilirar(boolean habilitar) {
        txtRuc.setEnabled(habilitar);
        txtNombre.setEnabled(habilitar);
        txtDireccion.setEnabled(habilitar);
        txtTelefono.setEnabled(habilitar);
        txtFax.setEnabled(habilitar);
        txtMovil.setEnabled(habilitar);
        txtDirectico.setEnabled(habilitar);
        txtaSlogan.setEnabled(habilitar);
        txtUrl.setEnabled(habilitar);
        txtMail.setEnabled(habilitar);
    }

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            if (btnGuardar.getText().trim().compareTo("Guardar") == 0) {
                if (txtRuc.getText().trim().length() != 0 && txtNombre.getText().trim().length() != 0) {
                    if (cspyme.insertPyme(this.getPyme())) {
                        showPyme();//kk
                        JOptionPane.showMessageDialog(null, "Datos de la compañía guardados correctamente ...", "Guardar", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "Los datos ingresados no se han podido guardar ...", "Guardar", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Los campos Ruc y Nombre de la empresa no pueden estar vacios ...", "Guardar", JOptionPane.WARNING_MESSAGE);
                }
            } else {//up
                if (chkEditar.isSelected()) {
                    ///
                    if (txtRuc.getText().trim().length() != 0 && txtNombre.getText().trim().length() != 0) {
                        if (cspyme.updatePyme(this.getPyme()))//cambiar x update
                        {
                            showPyme();//kk
                            JOptionPane.showMessageDialog(null, "Datos de la compañía actualizados correctamente ...", "Actualizar", JOptionPane.INFORMATION_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(null, "Los datos ingresados no se han podido actualizar ...", "Actualizar", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Los campos Ruc y Nombre de la empresa no pueden estar vacios ...", "Actualizar", JOptionPane.WARNING_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Para modificar y actualizar, primero debe marcar la casilla Editar ...", "Actualizar", JOptionPane.WARNING_MESSAGE);
                }
            }
        } catch (Exception ex) {
            ex.getMessage();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void chkEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkEditarActionPerformed

        if (chkEditar.isSelected()) {
            /*
             * txtRuc.setText("".trim()); txtNombre.setText("".trim());
             * txtDireccion.setText("".trim()); txtTelefono.setText("".trim());
             * txtFax.setText("".trim()); txtMovil.setText("".trim());
             * txtDirectico.setText("".trim()); txtaSlogan.setText("".trim());
             * txtUrl.setText("".trim());
            txtMail.setText("".trim());
             */
            setHabilirar(true);

        } else {
            setHabilirar(false);
        }
    }//GEN-LAST:event_chkEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JCheckBox chkEditar;
    private javax.swing.JScrollPane jScrollPanelTextoOpcional;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblDirectivo;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFax;
    private javax.swing.JLabel lblMovil;
    private javax.swing.JLabel lblPyme;
    private javax.swing.JLabel lblRuc;
    private javax.swing.JLabel lblSlogan;
    private javax.swing.JLabel lblTelefono;
    private javax.swing.JLabel lblUrl;
    private javax.swing.JPanel pnlBase;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtDirectico;
    private javax.swing.JTextField txtFax;
    private javax.swing.JTextField txtMail;
    private javax.swing.JTextField txtMovil;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtRuc;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtUrl;
    private javax.swing.JTextArea txtaSlogan;
    // End of variables declaration//GEN-END:variables
}
